package ps.client.plugin.eq2.gui.dialog.curses;

import javax.swing.DefaultListModel;

@SuppressWarnings("serial")
public class PlayerListModel extends DefaultListModel {
	
	public int addPlayer(String name, int recast) {
		int index = size();
		add(index, "\"" + name + "\" (" + recast + ")");
		return index;
	}
	
	public String getPlayer(int index) {
		if (index >= 0) {
			String str = (String)getElementAt(index);
			int i1 = str.indexOf('"') + 1;
			int i2 = str.indexOf('"', i1);
			return str.substring(i1, i2);
//			return str.substring(1, str.length() - 1);
		} else {
			return null;
		}
	}
	
	public int getRecast(int index) {
        if (index >= 0) {
            String str = (String)getElementAt(index);
            int i1 = str.indexOf('(') + 1;
            int i2 = str.indexOf(')', i1);
            return Integer.parseInt(str.substring(i1, i2));
        }
        return -1;
    }
	
	public int getRecastOfPlayer(String name) {
	    int index = indexOfPlayer(name);
	    if (index >= 0) {
            String str = (String)getElementAt(index);
            int i1 = str.indexOf('(') + 1;
            int i2 = str.indexOf(')', i1);
            return Integer.parseInt(str.substring(i1, i2));
	    }
	    return -1;
	}
	
	public void setRecast(int index, int recast) {
	    if (index >= 0) {
	        String str = (String)getElementAt(index);
            str = str.substring(0, str.indexOf('(')) + "(" + recast + ")";
            set(index, str);
	    }
	}
	
	public boolean containsPlayer(String name) {
	    int index = indexOfPlayer(name);
        if (index >=0) {
            return true;
        }
        return false;
//		return contains("\"" + name + "\"");
	}
	
	public boolean removePlayer(String name) {
	    int index = indexOfPlayer(name);
	    if (index >=0) {
	        removeElementAt(index);
	        return true;
	    }
	    return false;
//		return removeElement("\"" + name + "\"");
	}
	
	public int indexOfPlayer(String name) {
	    for (int i = 0; i < getSize(); i++) {
	        String str = (String)getElementAt(i);
	        if (str.contains("\"" + name + "\"")) {
	            return i;
	        }
        }
	    
	    return -1;
//		return indexOf("\"" + name + "\"");
	}
}
